//
//  FFDynamicFormManager.m
//  FFSurveillance
//
//  Created by roro_latrobe on 16/11/1.
//  Copyright © 2016年 cn.wanda. All rights reserved.
//

#import "FFDynamicFormManager.h"
#import "FFDynamicFormFactory.h"
#import "FFDynamicForm.h"
//#import <XLForm/XLForm.h>

@interface FFDynamicFormManager()<XLFormDescriptorDelegate>

@property (nonatomic, strong) XLFormViewController *formVc;

@end

@implementation FFDynamicFormManager

//FF_DEF_SINGLETON1(FFDynamicFormManager);

+ (FFDynamicFormManager *)sharedManager{
    static FFDynamicFormManager *kInstance = nil;
    @synchronized(self){
        if(!kInstance){
            kInstance = [[FFDynamicFormManager alloc] init];
        }
    }
    return kInstance;
}

- (void)pushDynamicFormWithConfigName:(NSString *)configName {
    //! 1. 先获取配置的数据
    FFDynamicFormFactory *factory = [FFDynamicFormFactory sharedFactory];
    FFDynamicForm *dynamicFormData = [factory createDynamicForm:configName];
    //! 2. 构建formDescriptor
    self.formVc = [XLFormViewController new];
    
    XLFormDescriptor *dynamicFormDesc = [XLFormDescriptor formDescriptorWithTitle:dynamicFormData.formTitle];
    __block XLFormSectionDescriptor *sectionDesc;
    __block XLFormRowDescriptor *rowDesc;
    [dynamicFormData.fromSections enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FFDynamicSection *sectionData = (FFDynamicSection *)obj;
        sectionDesc = [XLFormSectionDescriptor formSectionWithTitle:sectionData.sectionTitle];
        [dynamicFormDesc addFormSection:sectionDesc];
        [sectionData.sectionRows enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FFDynamicRow *rowData = (FFDynamicRow *)obj;
            rowDesc = [XLFormRowDescriptor formRowDescriptorWithTag:rowData.rowTag rowType:rowData.rowType title:rowData.rowTitle];
            [sectionDesc addFormRow:rowDesc];
        }];
    }];
    self.formVc.form = dynamicFormDesc;
    self.formVc.form.delegate = self;
    //! 3. 显示页面
    [[XYRouter sharedInstance] mapKey:dynamicFormData.formMapKey toControllerInstance:self.formVc];
    [[XYRouter sharedInstance] openURLString:dynamicFormData.formMapKey];
    
}

#pragma mark - XLFormDescriptorDelegate
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
    [self.formVc formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
    if ([rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeBooleanSwitch]) {
        
//        [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:kDebugOptionKeyNamed(rowDescriptor.tag)];
    }
}

-(void)formSectionHasBeenRemoved:(XLFormSectionDescriptor *)formSection atIndex:(NSUInteger)index {}
-(void)formSectionHasBeenAdded:(XLFormSectionDescriptor *)formSection atIndex:(NSUInteger)index {}
-(void)formRowHasBeenAdded:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath {}
-(void)formRowHasBeenRemoved:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath {}
-(void)formRowDescriptorPredicateHasChanged:(XLFormRowDescriptor *)formRow
                                   oldValue:(id)oldValue
                                   newValue:(id)newValue
                              predicateType:(XLPredicateType)predicateType {}


@end
